package com.wjerp.tplus.server.api.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.wjerp.tplus.server.api.controller.base.BaseController;
import com.wjerp.tplus.server.api.request.AddInvoiceInfoRequest;
import com.wjerp.tplus.server.api.request.QueryInvoiceListRequest;
import com.wjerp.tplus.server.api.response.InvoiceInfoResponse;
import com.wjerp.tplus.server.api.response.PageDataResponse;
import com.wjerp.tplus.server.api.response.ValueEnumResponse;
import com.wjerp.tplus.server.application.biz.InvoiceBiz;
import com.wjerp.tplus.server.common.exception.BusinessException;
import com.wjerp.tplus.server.common.exception.ServiceException;
import com.wjerp.tplus.server.common.model.BaseResponseData;
import com.wjerp.tplus.server.common.model.InvoiceCheckResponse;
import com.wjerp.tplus.server.common.security.JwtUser;
import com.wjerp.tplus.server.domain.entity.invoice.mng.InvoiceInfo;
import com.wjerp.tplus.server.domain.service.InvoiceInfoService;
import com.wjerp.tplus.server.domain.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lishuailei
 */
@Api(tags = "系统管理——发票管理",description = "系统管理——发票管理相关API")
@RestController
@RequestMapping("invoice")
public class InvoiceManage extends BaseController {

    @Resource
    private InvoiceBiz invoiceBiz;

    @Resource
    private InvoiceInfoService invoiceInfoService;

    @Resource
    private UserInfoService userInfoService;

    @ApiOperation("发票信息查询（分页）")
    @PostMapping("loadInvoiceInfoList")
    public ResponseEntity<BaseResponseData> loadInvoiceInfoList(@RequestBody QueryInvoiceListRequest request){
        PageDataResponse<InvoiceInfoResponse> pageDataResponse = invoiceBiz.loadInvoiceInfoList(request);
        return success(pageDataResponse);
    }

    @ApiOperation("发票内容详情查询")
    @GetMapping("loadInvoiceContentInfo/{invoiceUuidCode}")
    public ResponseEntity<BaseResponseData> loadInvoiceContentInfo(@PathVariable(value = "invoiceUuidCode") String invoiceUuidCode){
        InvoiceCheckResponse invoiceCheckResponse = invoiceInfoService.loadInvoiceContentInfoByInvoiceUuidCode(invoiceUuidCode);
        return success(invoiceCheckResponse);
    }

    @ApiOperation("删除发票")
    @DeleteMapping("deleteInvoice/{invoiceUuidCode}")
    public ResponseEntity<BaseResponseData> deleteInvoice(@PathVariable(value = "invoiceUuidCode") String invoiceUuidCode){
        JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
        try {
            invoiceInfoService.deleteInvoiceByCode(invoiceUuidCode,currentLoginUserInfo.getDisplayUserId());
            return success(true);
        } catch (ServiceException e) {
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        }
    }

    @ApiOperation("发票查验")
    @PutMapping("reVerInvoice/{invoiceUuidCode}")
    public ResponseEntity<BaseResponseData> reVerInvoice(@PathVariable(value = "invoiceUuidCode") String invoiceUuidCode){
        JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
        try {
            invoiceInfoService.reVerInvoice(invoiceUuidCode,currentLoginUserInfo.getCompanyCode(),currentLoginUserInfo.getDisplayUserId());
            return success(true);
        } catch (ServiceException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(),e.getErrorMsg(),e);
        }
    }

    @ApiOperation("新增发票信息")
    @PostMapping("addInvoiceInfo")
    public ResponseEntity<BaseResponseData> addInvoiceInfo(@RequestBody AddInvoiceInfoRequest request){
        try {
            JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
            request.setCompanyCode(currentLoginUserInfo.getCompanyCode());
            InvoiceInfoResponse invoiceInfoResponse = invoiceInfoService.addInvoiceInfo(request, currentLoginUserInfo.getDisplayUserId());
            if(invoiceInfoResponse != null){
                return fail("144444","发票信息已存在",invoiceInfoResponse);
            }
            return success(true);
        }catch (BusinessException ex){
            return fail(ex.getErrorCode(), ex.getErrorMsg(), ex);
        } catch (ServiceException e) {
            e.printStackTrace();
            return fail(e.getErrorCode(), e.getErrorMsg(), e);
        }
    }

    @ApiOperation("通过上传图片新增发票信息")
    @PostMapping("uploadImgToAddInvoiceInfo")
    public ResponseEntity<BaseResponseData> uploadImgToAddInvoiceInfo(
            @RequestParam("file") MultipartFile file){
        JwtUser currentLoginUserInfo = getCurrentLoginUserInfo();
        //TODO 待实现
        return success(true);
    }

    @ApiOperation("获取录入人员信息列表")
    @GetMapping("loadInputInvoiceMember")
    public ResponseEntity<BaseResponseData> loadInputInvoiceMember(){
        List<ValueEnumResponse> valueEnumResponseList = userInfoService.findAllUserInfoList();
        return success(valueEnumResponseList);
    }

}
